package com.weiwei.diancan.activity;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.weiwei.diancan.R;
import com.weiwei.diancan.http.ApiManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CardActivity extends AppCompatActivity {

    private Button bt_getcard;
    private TextView tv_show_card;
    private Retrofit retrofit;
    private ApiManager apiManager;
    private Map<String,String> cardMap = new HashMap<>();
    private EditText et_input;
    private SimpleDraweeView sv_bottom_banner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);

        bt_getcard = (Button)findViewById(R.id.bt_getcard);
        tv_show_card = (TextView)findViewById(R.id.tv_show_card);
        et_input = (EditText)findViewById(R.id.et_input);
        sv_bottom_banner = (SimpleDraweeView)findViewById(R.id.sv_bottom_banner);


        initData();

        retrofit = new Retrofit.Builder()
                .baseUrl("https://ccdcapi.alipay.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        apiManager = retrofit.create(ApiManager.class);

        et_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                getCardNo(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        bt_getcard.setOnClickListener(v -> {
            getCardNo(et_input.getText().toString());
        });
    }

    private void initData() {
        cardMap.put("SRCB","深圳农村商业银行");
        cardMap.put("BGB", "广西北部湾银行");
                cardMap.put("SHRCB", "上海农村商业银行");
                cardMap.put("BJBANK", "北京银行");
                cardMap.put( "WHCCB", "威海市商业银行");
                cardMap.put("BOZK", "周口银行");
                cardMap.put("KORLABANK", "库尔勒市商业银行");
                cardMap.put( "SPABANK", "平安银行");
                cardMap.put("SDEB", "顺德农商银行");
                cardMap.put("HURCB", "湖北省农村信用社");
                cardMap.put("WRCB", "无锡农村商业银行");
                cardMap.put("BOCY", "朝阳银行");
                cardMap.put("CZBANK", "浙商银行");
                cardMap.put("HDBANK", "邯郸银行");
                cardMap.put("BOC", "中国银行");
                cardMap.put("BOD", "东莞银行");
                cardMap.put( "CCB", "中国建设银行");
                cardMap.put( "ZYCBANK", "遵义市商业银行");
                cardMap.put("SXCB", "绍兴银行");
                cardMap.put("GZRCU", "贵州省农村信用社");
                cardMap.put("ZJKCCB", "张家口市商业银行");
                cardMap.put("BOJZ", "锦州银行");
                cardMap.put("BOP", "平顶山银行");
                cardMap.put("HKB", "汉口银行");
                cardMap.put("SPDB", "上海浦东发展银行");
                cardMap.put("NXRCU", "宁夏黄河农村商业银行");
                cardMap.put("NYNB", "广东南粤银行");
                cardMap.put("GRCB", "广州农商银行");
                cardMap.put("BOSZ", "苏州银行");
                cardMap.put("HZCB", "杭州银行");
                cardMap.put("HSBK", "衡水银行");
                cardMap.put("HBC", "湖北银行");
                cardMap.put("JXBANK", "嘉兴银行");
                cardMap.put("HRXJB", "华融湘江银行");
                cardMap.put("BODD", "丹东银行");
                cardMap.put("AYCB", "安阳银行");
                cardMap.put("EGBANK", "恒丰银行");
                cardMap.put("CDB", "国家开发银行");
                cardMap.put( "TCRCB", "江苏太仓农村商业银行");
                cardMap.put("NJCB", "南京银行");
                cardMap.put("ZZBANK", "郑州银行");
                cardMap.put("DYCB", "德阳商业银行");
                cardMap.put("YBCCB", "宜宾市商业银行");
                cardMap.put("SCRCU", "四川省农村信用");
                cardMap.put("KLB", "昆仑银行");
                cardMap.put("LSBANK", "莱商银行");
                cardMap.put("YDRCB", "尧都农商行");
                cardMap.put("CCQTGB", "重庆三峡银行");
                cardMap.put("FDB", "富滇银行");
                cardMap.put("JSRCU", "江苏省农村信用联合社");
                cardMap.put("JNBANK", "济宁银行");
                cardMap.put("CMB", "招商银行");
                cardMap.put("JINCHB", "晋城银行JCBANK");
                cardMap.put("FXCB", "阜新银行");
                cardMap.put("WHRCB", "武汉农村商业银行");
                cardMap.put("HBYCBANK", "湖北银行宜昌分行");
                cardMap.put("TZCB", "台州银行");
                cardMap.put("TACCB", "泰安市商业银行");
                cardMap.put("XCYH", "许昌银行");
                cardMap.put("CEB", "中国光大银行");
                cardMap.put("NXBANK", "宁夏银行");
                cardMap.put("HSBANK", "徽商银行");
                cardMap.put("JJBANK", "九江银行");
                cardMap.put("NHQS", "农信银清算中心");
                cardMap.put("MTBANK", "浙江民泰商业银行");
                cardMap.put("LANGFB", "廊坊银行");
                cardMap.put("ASCB", "鞍山银行");
                cardMap.put("KSRB", "昆山农村商业银行");
                cardMap.put("YXCCB", "玉溪市商业银行");
                cardMap.put("DLB", "大连银行");
                cardMap.put("DRCBCL", "东莞农村商业银行");
                cardMap.put("GCB", "广州银行");
                cardMap.put("NBBANK", "宁波银行");
                cardMap.put("BOYK", "营口银行");
                cardMap.put( "SXRCCU", "陕西信合");
                cardMap.put("GLBANK", "桂林银行");
                cardMap.put("BOQH", "青海银行");
                cardMap.put("CDRCB", "成都农商银行");
                cardMap.put("QDCCB", "青岛银行");
                cardMap.put("HKBEA", "东亚银行");
                cardMap.put( "HBHSBANK", "湖北银行黄石分行");
                cardMap.put("WZCB", "温州银行");
                cardMap.put("TRCB", "天津农商银行");
                cardMap.put( "QLBANK", "齐鲁银行");
                cardMap.put("GDRCC", "广东省农村信用社联合社");
                cardMap.put("ZJTLCB", "浙江泰隆商业银行");
                cardMap.put("GZB", "赣州银行");
                cardMap.put("GYCB", "贵阳市商业银行");
                cardMap.put("CQBANK", "重庆银行");
                cardMap.put("DAQINGB", "龙江银行");
                cardMap.put( "CGNB", "南充市商业银行");
                cardMap.put("SCCB", "三门峡银行");
                cardMap.put("CSRCB", "常熟农村商业银行");
                cardMap.put("SHBANK", "上海银行");
                cardMap.put( "JLBANK", "吉林银行");
                cardMap.put("CZRCB", "常州农村信用联社");
                cardMap.put("BANKWF", "潍坊银行");
                cardMap.put("ZRCBANK", "张家港农村商业银行");
                cardMap.put("FJHXBC", "福建海峡银行");
                cardMap.put("ZJNX", "浙江省农村信用社联合社");
                cardMap.put("LZYH", "兰州银行");
                cardMap.put( "JSB", "晋商银行");
                cardMap.put("BOHAIB", "渤海银行");
                cardMap.put("CZCB", "浙江稠州商业银行");
                cardMap.put("YQCCB", "阳泉银行");
                cardMap.put("SJBANK", "盛京银行");
                cardMap.put("XABANK", "西安银行");
                cardMap.put("BSB", "包商银行");
                cardMap.put("JSBANK", "江苏银行");
                cardMap.put("FSCB", "抚顺银行");
                cardMap.put("HNRCU", "河南省农村信用");
                cardMap.put("COMM", "交通银行");
                cardMap.put( "XTB", "邢台银行");
                cardMap.put("CITIC", "中信银行");
                cardMap.put( "HXBANK", "华夏银行");
                cardMap.put("HNRCC", "湖南省农村信用社");
                cardMap.put( "DYCCB", "东营市商业银行");
                cardMap.put("ORBANK", "鄂尔多斯银行");
                cardMap.put("BJRCB", "北京农村商业银行");
                cardMap.put( "XYBANK", "信阳银行");
                cardMap.put("ZGCCB", "自贡市商业银行");
                cardMap.put( "CDCB", "成都银行");
                cardMap.put("HANABANK", "韩亚银行");
                cardMap.put("CMBC", "中国民生银行");
                cardMap.put("LYBANK", "洛阳银行");
                cardMap.put("GDB", "广东发展银行");
                cardMap.put( "ZBCB", "齐商银行");
                cardMap.put( "CBKF", "开封市商业银行");
                cardMap.put("H3CB", "内蒙古银行");
                cardMap.put("CIB", "兴业银行");
                cardMap.put("CRCBANK", "重庆农村商业银行");
                cardMap.put("SZSBK", "石嘴山银行");
                cardMap.put("DZBANK", "德州银行");
                cardMap.put("SRBANK", "上饶银行");
                cardMap.put("LSCCB", "乐山市商业银行");
                cardMap.put("JXRCU", "江西省农村信用");
                cardMap.put( "ICBC", "中国工商银行");
                cardMap.put( "JZBANK", "晋中市商业银行");
                cardMap.put( "HZCCB", "湖州市商业银行");
                cardMap.put( "NHB", "南海农村信用联社");
                cardMap.put("XXBANK", "新乡银行");
                cardMap.put("JRCB", "江苏江阴农村商业银行");
                cardMap.put( "YNRCC", "云南省农村信用社");
                cardMap.put( "ABC", "中国农业银行");
                cardMap.put("GXRCU", "广西省农村信用");
                cardMap.put("PSBC", "中国邮政储蓄银行");
                cardMap.put("BZMD", "驻马店银行");
                cardMap.put("ARCU", "安徽省农村信用社");
                cardMap.put("GSRCU", "甘肃省农村信用");
                cardMap.put("LYCB", "辽阳市商业银行");
                cardMap.put( "JLRCU", "吉林农信");
                cardMap.put("URMQCCB", "乌鲁木齐市商业银行");
                cardMap.put("XLBANK", "中山小榄村镇银行");
                cardMap.put( "CSCB", "长沙银行");
                cardMap.put("JHBANK", "金华银行");
                cardMap.put( "BHB", "河北银行");
                cardMap.put("NBYZ", "鄞州银行");
                cardMap.put("LSBC", "临商银行");
                cardMap.put("BOCD", "承德银行");
                cardMap.put( "SDRCU", "山东农信");
                cardMap.put("NCB", "南昌银行");
                cardMap.put("TCCB", "天津银行");
                cardMap.put( "WJRCB", "吴江农商银行");
                cardMap.put( "CBBQS", "城市商业银行资金清算中心");
                cardMap.put("HBRCU", "河北省农村信用社");
    }

    private void getCardNo(String card){
        apiManager.getCard("utf-8",card,true)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            String s = responseBody.string().toString();
                            JSONObject jsonObject = new JSONObject(s);
                            String bank = jsonObject.getString("bank");

                            if(cardMap.get(bank)!=null) {
                                tv_show_card.setText(cardMap.get(bank));
                                String ht = "https://apimg.alipay.com/combo.png?d=cashier&t="+bank;
                                sv_bottom_banner.setImageURI(Uri.parse(ht));
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
